#include "data_global.h"
#include "leddriver.h"

// A9 LED模块线程

extern pthread_cond_t  cond_led;
extern pthread_mutex_t  mutex_led;
extern struct makeru_ctrl_data ctrl_data[MONITOR_NUM];

void *pthread_led(void *arg)
{
    int fd = -1, i = 0;
    printf("pthread_led\n");

    if((fd = open(LED_DEV, O_RDWR)) == -1)
    {
        perror("led open");
        exit(-1);
    }
	
	while(1)
	{
		pthread_mutex_lock(&mutex_led);
		pthread_cond_wait(&cond_led, &mutex_led);
		printf("led ctrl ok! %d \n", ctrl_data[HOME_ID].ledstate);
		if(ctrl_data[HOME_ID].ledstate <= 1)
		{
			if(ctrl_data[HOME_ID].ledstate) for(i=2; i<=5; i++) ioctl(fd, LED_IOCTL_ON, i);
			else for(i=2; i<=5; i++) ioctl(fd, LED_IOCTL_OFF, i);
		}
		else
		{
			ioctl(fd, LED_IOCTL_TOGGLE, ctrl_data[HOME_ID].ledstate);
		}
		pthread_mutex_unlock(&mutex_led);
	}
	close(fd);
#if 0
	5.	open(dev_led,  )
	6.	pthread_cond_wait (cond_led,  );
	7.	获取dev_led_mask（控制标志）
	8.	通过ioctl（）控制led
#endif 
}